﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using Microsoft.Win32;
using RedFlag.Engine;
using System.Text.RegularExpressions;

namespace RedFlag
{
    
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            string launchProg=null;
            string launchArgs = String.Empty;
            bool ProcessArrays=false;
            int StackLength=5;
            int ObjectDepth=4;
            string setBreakpoint = String.Empty;
            // This is for debugging services when we are called from Image File Execution Options.
           // System.Threading.Thread.Sleep(12000);
            if (args.Length > 1 && args[0].StartsWith("/Pipe:", StringComparison.OrdinalIgnoreCase))
            {
                string pipeName = String.Empty; string programName = String.Empty; string programArgs = String.Empty;
                pipeName = args[0].Substring(args[0].IndexOf(":") + 1);
                programName = args[1];
                if (args.Length > 2)
                {
                    Regex regCmdLn = new Regex(@"([a-zA-Z]{1}:\\.*\.[a-zA-Z]{3}.*)");
                    Match match = regCmdLn.Match(Environment.CommandLine.Substring(3));
                    // should return everything Win put in IFEO
                    string IFEOProgandArgs = match.Value;
                    Regex regCmdArg = new Regex(@"(\.[a-zA-Z]{3}[^\\].*)");
                    Match match2 = regCmdArg.Match(IFEOProgandArgs.Substring(3));
                    string temp = String.Empty;
                    temp = match2.Value;
                    programArgs = temp.Substring(4).TrimStart(new char[] { '\"', ' ' });
                }
               // programName = null;
              //  int rfCommandLength=System.Reflection.Assembly.GetExecutingAssembly().Location.Length+args[0].Length+4;
                //programArgs = Environment.CommandLine.Substring(rfCommandLength);
                RedFlagServer server = new RedFlagServer(pipeName, programName, programArgs);
                server.Run();
            }
            else
            {
                foreach (string arg in args)
                {
                    if (arg.ToUpper().StartsWith("/LAUNCH:")) launchProg = arg.Substring(8).Trim('\"');
                    if (arg.ToUpper().StartsWith("/ARGUMENTS:")) launchArgs = arg.Substring(11).Trim('\"');
                    if (arg.ToUpper().StartsWith("/OBJECTDEPTH:")) ObjectDepth = Convert.ToInt32(arg.Substring(13).Trim('\"'));
                    if (arg.ToUpper().StartsWith("/STACKLENGTH:")) StackLength = Convert.ToInt32(arg.Substring(13).Trim('\"'));
                    if (arg.ToUpper().StartsWith("/BREAKPOINT:")) setBreakpoint = arg.Substring(12).Trim('\'');
                    if (arg.ToUpper() == "/PROCESSARRAYS") ProcessArrays = true;
                }

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                if (launchProg == null) Application.Run(new Form1(ProcessArrays, StackLength, ObjectDepth, setBreakpoint));
                else Application.Run(new Form1(launchProg, launchArgs, ProcessArrays, StackLength, ObjectDepth, setBreakpoint));
            }
            }
        /// <summary>
        /// Copy the whole list in this case as text
        /// </summary>
        /// <param name="lb"></param>
        public static void CopyListBoxToClipboard(ListBox lb)
        {
            StringBuilder buffer = new StringBuilder();

            for (int i = 0; i < lb.Items.Count; i++)
            {
                buffer.Append(lb.Items[i].ToString());
                buffer.Append("\r\n");
            }

            Clipboard.SetText(buffer.ToString());
        }
        /// <summary>
        /// The LV is mighty large--copy only the selected item
        /// </summary>
        /// <param name="lb"></param>
        public static void CopyListViewToClipboard(ListView lv)
        {
            StringBuilder buffer = new StringBuilder();
            ListView.SelectedListViewItemCollection coll = lv.SelectedItems;
            foreach (ListViewItem item in coll)
            {
                foreach (System.Windows.Forms.ListViewItem.ListViewSubItem subItem in item.SubItems)
                {
                    buffer.Append(subItem.Text+"\t");
                }
                buffer.Append("\r\n");

            }
            Clipboard.SetText(buffer.ToString());
        }  

    }
}
